package com.flanz.apache.velocity.uimocker;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * MimeTable.
 * 
 * @author cf
 * 
 */
public class MimeTable {

	private Properties instanceMAP = null;
	public MimeTable(String fileName) throws FileNotFoundException {
		FileInputStream fileInputStream = new FileInputStream(fileName);
		instanceMAP = loadMap(fileInputStream);
	}

	private static String getMimeType(Properties mimeMapping, File fromFileName) {
		String result = "application/octet-stream";
		String name = fromFileName.getName();
		int lastIndexOf = name.lastIndexOf('.');
		if (lastIndexOf != -1) {
			String match = name.substring(lastIndexOf + 1);
			Object object = mimeMapping.get(match);
			if (object != null) {
				result = object.toString();
			}

		}

		return result;
	}

	public String getMimeType(File fromFileName) {
		return getMimeType(instanceMAP, fromFileName);
	}

	private static Properties loadMap(InputStream resourceAsStream) {
		Properties properties = new Properties();
		try {
			properties.load(resourceAsStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}

}
